﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bg.krasi.caroffers;
using System.Text;
using CarsWebApplication.businesslogic.utils;
using CarsWebApplication.businesslogic.entities;
using CarsWebApplication.businesslogic.constants;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
    private MobileOffersService mobileOffersService;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.mobileOffersService == null)
        {
            this.mobileOffersService = new MobileOffersService();
            CarWebAppUtility.SetMobileOffersWSUrl(this.mobileOffersService);
        }
        if (IsPostBack == false)
        {            
            string allBrands = mobileOffersService.getAllBrands();
            List<Brand> brands = CarWebAppUtility.GetBrands(allBrands);
            this.brandsDropDown.DataSource = brands;
            this.brandsDropDown.DataBind();
        }
    }

    protected void SearchCarOffers_OnClick(object sender, EventArgs e)
    {        
        string brandIdTxt = this.brandsDropDown.SelectedValue;
        int brandId;
        bool validBrandId = Int32.TryParse(brandIdTxt, out brandId);
        string modelIdTxt = this.modelsDropDown.SelectedValue;
        int modelId;
        bool validModelId = Int32.TryParse(modelIdTxt, out modelId);
        string yearDownTxt = this.yearDownTxt.Text;
        int yearDown;
        bool validYearDown = Int32.TryParse(yearDownTxt, out yearDown);
        string yearUpTxt = this.yearUpTxt.Text;
        int yearUp;
        bool validYearUp = Int32.TryParse(yearUpTxt, out yearUp);
        bool validYears = (validYearDown && validYearUp && yearDown <= yearUp);
        string priceDownTxt = this.priceDownTxt.Text;
        double priceDown;
        bool validPriceDown = Double.TryParse(priceDownTxt, out priceDown);
        string priceUpTxt = this.priceUpTxt.Text;
        double priceUp;
        bool validPriceUp = Double.TryParse(priceUpTxt, out priceUp);
        bool validPrices = (validPriceDown && validPriceUp && priceDown <= priceUp);
        if (validBrandId && validModelId && validYears && validPrices)
        {
            string carOffers = this.mobileOffersService.getOffersByFilter(brandId, modelId, yearUp, 
                yearDown, priceUp, priceDown);
            List<Offer> foundOffers =  CarWebAppUtility.GetOffers(carOffers);
            this.carOffersGrid.DataSource = foundOffers;
            this.carOffersGrid.DataBind();
            HideValidationErrors();
        }
        else
        {
            ShowValidationErrors(validYears, validPrices);
        }
    }

    protected void Brands_IndexChanged(object sender, EventArgs e)
    {
        string brandIdTxt = this.brandsDropDown.SelectedValue;
        int brandId;
        if(Int32.TryParse(brandIdTxt, out brandId))
        {
            if (brandId >= 0)
            {
                string modelsByBrand = this.mobileOffersService.getModelsByBrand(brandId);
                if (string.IsNullOrEmpty(modelsByBrand) == false)
                {
                    List<Model> models = CarWebAppUtility.GetModels(modelsByBrand);
                    this.modelsDropDown.DataSource = models;
                    this.modelsDropDown.DataBind();
                }
            }
        }
    }

    protected void EditCarOfferEventHandler_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }

    protected void CarOffersGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.carOffersGrid.PageIndex = e.NewPageIndex;
        this.carOffersGrid.DataBind();
        HideValidationErrors();
    }

    protected void Year_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        int year;
        e.IsValid = CarWebAppUtility.IsValidYear(e.Value, out year);
    }

    protected void Price_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        double year;
        e.IsValid = CarWebAppUtility.IsValidPrice(e.Value, out year);
    }

    protected void Id_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        int id;
        e.IsValid = CarWebAppUtility.IsValidId(e.Value, out id);
    }

    private void ShowValidationErrors(bool validYears, bool validPrices)
    {
        this.notificationMsgList.Items.Clear();
        if (validYears == false)
        {
            AddNotificationMsg("Make year period is not valid");
        }
        if (validPrices == false)
        {
            AddNotificationMsg("Price range is not valid");
        }
        this.notificationMsgPanel.Visible = true;
    }

    private void HideValidationErrors()
    {
        this.notificationMsgList.Visible = false;
    }

    private void AddNotificationMsg(string notificationMsg)
    {
        ListItem listItem = new ListItem(notificationMsg);
        this.notificationMsgList.Items.Add(listItem);
    }
}
